import { createStore } from 'vuex'

let state = {
  // token 信息对象
  tokenInfo: {}
}

const stateStr = window.localStorage.getItem('state')
if (stateStr) {
  state = JSON.parse(stateStr)
}

export default createStore({
  state,
  mutations: {
    // 更新 token 信息
    updateTokenInfo(state, payload) {
      state.tokenInfo = payload
      this.commit('saveToStorage')
    },
    // 把 vuex 数据持久化存储到本地
    saveToStorage(state) {
      window.localStorage.setItem('state', JSON.stringify(state))
    }
  }
})
